package opennlp.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import opennlp.tools.lang.english.SentenceDetector;
import opennlp.tools.lang.english.Tokenizer;
import opennlp.tools.lang.english.TreebankParser;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.ParserME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.util.Span;

public class Main {
	public static void main(String[] args) throws IOException {
		String paragraph  = "...";
		 
//		 the sentence detector and tokenizer constructors
//		 take paths to their respective models
		SentenceDetectorME sdetector =
		    new SentenceDetector("N:/java/_libs/opennlp-tools-1.3.0/model/EnglishSD.bin.gz");
		Tokenizer tokenizer = new Tokenizer("N:/java/_libs/opennlp-tools-1.3.0/model/EnglishTok.bin.gz");
		 
//		 the parser takes the path to the parser models
//		 directory and a few other options
		boolean useTagDict = true;
		boolean useCaseInsensitiveTagDict = false;
		int beamSize = ParserME.defaultBeamSize;
		double advancePercentage = ParserME.defaultAdvancePercentage;
		ParserME parser = TreebankParser.getParser(
		        "N:/java/_libs/opennlp-tools-1.3.0/model/", useTagDict, useCaseInsensitiveTagDict,
		        beamSize, advancePercentage);
		 
//		 break a paragraph into sentences
		String[] sents = sdetector.sentDetect("A musician is a person who plays or composes music.");
		
		for(String str: sents) {
			System.out.println(str);
		}
		
		
		String sent = sents[0];
		 
//		 tokenize brackets and parentheses by putting a space on either side.
//		 this makes sure it doesn't get confused with output from the parser
		//sent = untokenizedParenPattern1.matcher(sent).replaceAll("$1 $2");
		//sent = untokenizedParenPattern2.matcher(sent).replaceAll("$1 $2");
		 
//		 get the tokenizer to break apart the sentence
		String[] tokens = tokenizer.tokenize(sent);
		 
//		 build a string to parse as well as a list of tokens
		StringBuffer sb = new StringBuffer();
		List<String> tokenList = new ArrayList<String>();
		for (int j = 0; j < tokens.length; j++)
		{
		    String tok =tokens[j]; //convertToken(tokens[j]);
		    System.out.println("token = "+tok);
		    tokenList.add(tok);
		    sb.append(tok).append(" ");
		}
		String text = sb.substring(0, sb.length() - 1).toString();
	
		
		
		// the parent parse instance spans the entire sentence
		Parse p = new Parse(text, new Span(0, text.length()), "INC", 1, null);

		// create a parse object for each token and add it to the parent
		int start = 0;
		for (Iterator ti = tokenList.iterator(); ti.hasNext();) {
			String tok = (String) ti.next();
			p.insert(new Parse(text, new Span(start, start + tok.length()),
					ParserME.TOK_NODE, 0));
			start += tok.length() + 1;
		}
		p.show();
		// fetch multiple possible parse trees
		Parse[] parses = parser.parse(p, 3); //numParses

			StringBuffer sb2 = new StringBuffer();
			build(parses[0], sb2);
			
			System.out.println(sb2.toString());

	}
	
	public static void build(Parse pp, StringBuffer sb) {

			    int start;
			    start = pp.getSpan().getStart();
			    if (!pp.getType().equals(ParserME.TOK_NODE)) {
			      sb.append("<"+pp.getType()+">");
	
			    }
			    for (Parse c : pp.getChildren()) {
			      Span s = c.getSpan();
			      if (start < s.getStart()) {
			        //System.out.println("pre "+start+" "+s.getStart());
			        sb.append(pp.getText().substring(start, s.getStart()));
			      }
			      build(c ,sb);
			      start = s.getEnd();
			    }
			    sb.append(pp.getText().substring(start, pp.getSpan().getEnd()));
			    if (!pp.getType().equals(ParserME.TOK_NODE)) {
			    	 sb.append("</"+pp.getType()+">");
			    }
			
	}
}
